package com.starry.module.system.core.user.controller.admin;

import cn.hutool.http.HttpStatus;
import com.starry.core.common.domain.R;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.core.web.core.controller.BaseController;
import com.starry.module.system.api.user.domain.qo.SysUserLoginQo;
import com.starry.module.system.api.user.domain.qo.SysUserPageQo;
import com.starry.module.system.api.user.domain.qo.SysUserPasswordQo;
import com.starry.module.system.api.user.domain.qo.SysUserQo;
import com.starry.module.system.api.user.domain.vo.SysUserDtlVo;
import com.starry.module.system.api.user.domain.vo.SysUserLoginInfoVo;
import com.starry.module.system.api.user.domain.vo.SysUserVo;
import com.starry.module.system.core.user.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 系统用户
 *
 * @author xiaoke
 */
@Tag(name = "系统用户")
@RestController
@RequestMapping("/system/sysUser")
public class SysUserController extends BaseController {

    private final SysUserService sysUserService;

    public SysUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 分页查询
     *
     * @param pageQo 查询实体
     * @return 所有数据
     */
    @Operation(summary = "系统用户-分页查询")
    @GetMapping("sysUserPage")
    public R<PageVo<SysUserVo>> findPage(SysUserPageQo pageQo) {
        return success(this.sysUserService.page(pageQo));
    }


    /**
     * 列表查询
     *
     * @param pageQo 查询实体
     * @return 所有数据
     */
    @Operation(summary = "系统用户-列表查询")
    @GetMapping("sysUserList")
    public R<List<SysUserVo>> findList(SysUserPageQo pageQo) {
        return success(this.sysUserService.findList(pageQo));
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "系统用户-详情")
    @GetMapping("sysUserDetails/{id}")
    public R<SysUserDtlVo> findById(@PathVariable("id") Long id) {
        return success(this.sysUserService.getByIdDtl(id));
    }

    /**
     * 根据当前登录用户-获取详情
     *
     * @return 单条数据
     */
    @Operation(summary = "系统用户-根据当前登录用户-获取详情")
    @GetMapping("sysUserInfo")
    public R<SysUserLoginInfoVo> findByLoginUserInfo() {
        return success(sysUserService.getByLoginUserInfo());
    }

    /**
     * 新增数据
     *
     * @param sysUserQo 实体对象
     * @return 新增结果
     */
    @Operation(summary = "系统用户-新增")
    @PostMapping("sysUserCreate")
    public R<Long> insert(@RequestBody @Validated SysUserQo sysUserQo) {
        return success(HttpStatus.HTTP_CREATED, this.sysUserService.save(sysUserQo));
    }

    /**
     * 修改数据
     *
     * @param sysUserQo 实体对象
     * @return 修改结果
     */
    @Operation(summary = "系统用户-修改")
    @PostMapping("sysUserUpdate")
    public R<Void> update(@RequestBody @Validated SysUserQo sysUserQo) {
        this.sysUserService.updateById(sysUserQo);
        return success();
    }

    /**
     * 修改当前登陆用户信息
     *
     * @param loginQo 实体对象
     */
    @Operation(summary = "系统用户-修改当前登陆用户信息")
    @PostMapping("sysUserUpdateLogin")
    public R<Void> updateLogin(@RequestBody @Validated SysUserLoginQo loginQo) {
        this.sysUserService.updateLogin(loginQo);
        return success();
    }

    /**
     * 修改当前登陆用户密码
     *
     * @param passwordQo 实体对象
     */
    @Operation(summary = "系统用户-修改当前登陆用户密码")
    @PostMapping("sysUserUpdatePassword")
    public R<Void> updatePassword(@RequestBody @Validated SysUserPasswordQo passwordQo) {
        this.sysUserService.updatePassword(passwordQo);
        return success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @Operation(summary = "系统用户-删除")
    @PostMapping("sysUserRemove")
    public R<Void> delete(@RequestParam("idList") Set<Long> idList) {
        this.sysUserService.removeByIds(idList);
        return success();
    }

    /**
     * 启用数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @PreAuthorize("hasAuthority('user:enabled')")
    @Operation(summary = "系统用户-启用")
    @PostMapping("sysUserEnable")
    public R<Void> enable(@RequestParam("idList") Set<Long> idList) {
        this.sysUserService.enable(idList);
        return success();
    }

    /**
     * 禁用数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @Operation(summary = "系统用户-禁用")
    @PostMapping("sysUserDisable")
    public R<Void> disable(@RequestParam("idList") Set<Long> idList) {
        this.sysUserService.disable(idList);
        return success();
    }
}

